package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqXzqArea;
import cloud.maque.admin.domain.vo.MqXzqAreaVo;
import cloud.maque.admin.mapper.MqXzqAreaMapper;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MqXzqAreaServiceImplHelper extends ServiceImpl<MqXzqAreaMapper, MqXzqArea> {


    @Autowired
    private StringRedisTemplate redisTemplate;


    public String GetReidsKey(String areaCode, String areaId, Boolean isAll) {
        //Redis缓存
        String rediskey = "gisq:xzqarea:" + areaCode;
        if (StrUtil.isEmptyIfStr(areaCode)) {
            rediskey = "gisq:xzqarea:" + areaId;
        }
        if (isAll) {
            rediskey += ":all";
        }

        return rediskey;
    }

    public List<MqXzqAreaVo> GetTreeRedis(Boolean fromredis, String redisKey) {


        if (fromredis) {
            Object objXzq = redisTemplate.opsForValue().get(redisKey);

            if (objXzq != null) {
                List<MqXzqAreaVo> tree = JSON.parseArray(objXzq.toString(), MqXzqAreaVo.class);
                return tree;
            } else {
                return null;
            }
        }

        return null;

    }

    public void SaveTreeRedis(String redisKey, List<MqXzqAreaVo> trees) {

        if (trees != null) {
            String jsonss = JSON.toJSONString(trees);
            redisTemplate.opsForValue().set(redisKey, jsonss);
        }

    }

    public List<MqXzqArea> listByParentId(String parentId) {

        QueryWrapper<MqXzqArea> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(MqXzqArea::getAreaParentId, parentId);
        queryWrapper.lambda().orderByAsc(MqXzqArea::getAreaOrder);
        List<MqXzqArea> list = this.baseMapper.selectList(queryWrapper);

        return list;
    }

    public List<MqXzqArea> listByCode(String areaCode) {

        QueryWrapper<MqXzqArea> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(MqXzqArea::getAreaCode, areaCode);
        queryWrapper.lambda().orderByAsc(MqXzqArea::getAreaOrder);
        List<MqXzqArea> list = this.baseMapper.selectList(queryWrapper);

        return list;
    }

    public List<MqXzqArea> listByAreaId(String areaId) {

        QueryWrapper<MqXzqArea> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(MqXzqArea::getAreaId, areaId);
        queryWrapper.lambda().orderByAsc(MqXzqArea::getAreaOrder);
        List<MqXzqArea> list = this.baseMapper.selectList(queryWrapper);

        return list;
    }

    public Integer GetSubCount(String parentId) {

        QueryWrapper<MqXzqArea> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(MqXzqArea::getAreaParentId, parentId);
        Integer list = this.baseMapper.selectCount(queryWrapper);

        return list;
    }
}
